import requests
import json


#获取袋子二维码的值
#todo 换服务器的话需要改url
class GetCode:
    def __init__(self):
        loginData = {"username": "rjzt", "password": "123456"}
        loginRes = requests.post('http://220.187.226.90:10081/mw/serve/login/login.php', data=loginData)
        # loginRes = requests.post('http://47.111.255.114/mw/serve/login/login.php', data=loginData)
        self.cookies = loginRes.cookies

    def httpGet(self, url):
        Res = requests.get(url)
        return Res

    def httpPost(self, url, data):
        l = requests.post("http://220.187.226.90:10081/mw/serve/login/login.php", '{"username":"rjzt","password":"123456"}')
        # l = requests.post("http://47.111.255.114/mw/serve/login/login.php", '{"username":"xwzt","password":"123456"}')
        cookies = l.cookies
        Res = requests.post(url, data=data, cookies=cookies)
        return Res

    def httpDelete(self, url, data):
        Res = requests.delete(url, data=data, cookies=self.cookies)
        return Res

    # 获取垃圾袋的code，index从0开始
    def get_bag_cod(self, index):
        res_text = self.httpPost("http://220.187.226.90:10081/mw/serve/data/track.php",
                                 '{"qrcode":"","name":"","department":"","type":"","status":"",'
                                 '"beginTime":"","endTime":"","beginHandoverTime":"",'
                                 '"endHandoverTime":"","currentPage":1}').text
        # res_text = self.httpPost("http://47.111.255.114/mw/serve/data/track.php",
        #                          '{"qrcode":"","name":"","department":"","type":"","status":"",'
        #                          '"beginTime":"","endTime":"","beginHandoverTime":"",'
        #                          '"endHandoverTime":"","currentPage":1}').text
        # print(res_text)
        res_dic = json.loads(res_text)
        bag_code = res_dic['data'][index]['garbageQRCode']
        # print(res_dic['data'][index]['garbageQRCode'])
        return bag_code


if __name__ == '__main__':
    comm = GetCode()
    a = comm.get_bag_cod(2)
    print(a)
    # print(comm.cookies)
    # res=comm.httpGet('http://192.168.1.246:8080/woniusales/')
    # print(res)
    #

    # res2=comm.httpPost("https://www.rjzhyl.com/mw/serve/login/login.php",'{"username":"rjzt","password":"123456"}')
    # print(res2.text)

    # res3=comm.httpPost("https://www.rjzhyl.com/mw/serve/data/track.php",'{"qrcode":"","name":"","department":"","type":"","status":"","beginTime":"","endTime":"","beginHandoverTime":"","endHandoverTime":"","currentPage":1}')
    # res_text=res3.text
    # res_dic=json.loads(res_text)
    # print(res_dic['data'][0]['garbageQRCode'])
    # print(res_dic['data'][1]['garbageQRCode'])

# 参考代码
# import requests
#
# class Common:
#     def __init__(self):
#         loginData={'username':'admin','password':'admin123','verifycode':'0000'}
#         loginRes=requests.post('http://192.168.1.246:8080/woniusales/user/login',data=loginData)
#         self.cookies=loginRes.cookies
#
#     def httpGet(self,url):
#         Res=requests.get(url)
#         return Res
#
#     def httpPost(self,url,data):
#         Res=requests.post(url,data=data,cookies=self.cookies)
#         return Res
#
#     def httpDelete(self,url,data):
#         Res=requests.delete(url,data=data,cookies=self.cookies)
#         return Res
#
# if __name__=='__main__':
#     comm=Common()
#     print(comm.cookies)
#     res=comm.httpGet('http://192.168.1.246:8080/woniusales/')
#     print(res)
